#include "authentication.h"
#include <QSettings>
#include <QDebug>
#include "Main/constant.h"
#include "Main/global.h"
#include <QCryptographicHash>
//#include "Main/global.h"
#include "Msg/Rsp/rspbase.h"

const QString NET_FILE = "SSLNetInfo.ini";
const QString CONFIG_FILE = "Config.ini";

Authentication::Authentication(QObject *parent) : QObject(parent)
{

    Init();
}

bool Authentication::Login(const QString qsUser, const QString qsPwd, QString& qsErrMsg)
{
    QString qsIP;
    int nPort;
    if (!GetNetAddress(qsIP, nPort))
    {
        qsErrMsg = "网络配置内容错误";
        return false;
    }

    m_comClient.Init(qsIP, nPort);

    sprintf(Global::Instance()->gReqHead.exch_code,"%s", "8006");
    sprintf(Global::Instance()->gReqHead.user_id,"%s", qsUser.toStdString().c_str());

    QString md5;
    QByteArray bb;
    bb = QCryptographicHash::hash(qsPwd.toLatin1(), QCryptographicHash::Md5 );
    md5.append(bb.toHex());
    qDebug()<< "md5:"<< md5;

    m_req8006.AddPara("user_id", qsUser);
    m_req8006.AddPara("user_pwd", md5);
    std::string sRsp;// = m_req8006.ToString();
    PACKET_INFO pktInfo(CHANNEL_AUTH);
    pktInfo.nEncr = 1;
    if (E_TRANS_SUCCESS == m_comClient.TransForNormal(pktInfo,m_req8006.ToString(), sRsp, qsErrMsg))
    {
        HEADER_RSP rspHead;
        rspHead.DecodeRspHead(sRsp.c_str());
        RspBase rspMsg;
        rspMsg.ParseRspBody(QString::fromLocal8Bit(sRsp.c_str()));
        if ( 0 == gc_ErrCodeSus.compare(rspHead.rsp_code))
        {
            Global::Instance()->ParseServerList(rspMsg.GetQStringPara("htm_server_list"));
            Global::Instance()->m_qsSessionID = rspMsg.GetQStringPara("session_id");
            qDebug()<<"SessionID"<<Global::Instance()->m_qsSessionID;
            return true;
        }
        else
        {
            qsErrMsg = rspMsg.GetQStringPara("rsp_msg");
        }
    }

    return false;
}

void Authentication::Init()
{
    std::shared_ptr<QSettings> iniSSLNetInfo = std::make_shared<QSettings>(NET_FILE, QSettings::IniFormat);
    if (iniSSLNetInfo.get() != nullptr)
    {
        QString servers = iniSSLNetInfo->value("ServerName/all_server").toString();
        qDebug()<< "servers:"<<servers;
        QStringList serverList = servers.split(',');
        m_qsServerName = serverList.at(0);
        qDebug()<< "m_qsServerName:"<<m_qsServerName;
    }

    m_req8006.AddPara("oper_flag", 1);
    m_req8006.AddPara("user_id_type", 1);
    m_req8006.AddPara("net_agent", 1);
    m_req8006.AddPara("net_envionment", 1);
//    m_req8006.AddPara("bank_no", "");
    m_req8006.AddPara("user_type", gc_sUserTypeOper.c_str());
    m_req8006.AddPara("login_ip", Global::Instance()->GetHostIpAddress());
    std::shared_ptr<QSettings> iniConfig = std::make_shared<QSettings>(CONFIG_FILE, QSettings::IniFormat);
    if (iniConfig.get())
    {
        m_req8006.AddPara("net_envionment", iniConfig->value("LOGIN/net_envionment").toInt());
    }
}

bool Authentication::GetNetAddress(QString& qsIP, int &nPort)
{
    std::shared_ptr<QSettings> iniSSLNetInfo = std::make_shared<QSettings>(NET_FILE, QSettings::IniFormat);
    if (iniSSLNetInfo.get())
    {
        qsIP = iniSSLNetInfo->value(m_qsServerName+"/SSLServerHost").toString();
        nPort = iniSSLNetInfo->value(m_qsServerName+"/SSLServerPort").toInt();
        if (qsIP.size() == 0 || nPort == 0)
        {
            qDebug()<< "网络配置内容错误:";
            return false;
        }
        else
            return true;
    }

    qDebug()<< "无法读取配置文件:"<<NET_FILE;
    return false;
}

